<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input type="text" v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="支出团队" size="small">
            <el-autocomplete
              v-model="searchForm.outTeamName"
              :fetch-suggestions="querySearchFormOutTeam"
              @select="handleSelectFormOutTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="收入团队" size="small">
            <el-autocomplete
              v-model="searchForm.inTeamName"
              :fetch-suggestions="querySearchFormInTeam"
              @select="handleSelectFormInTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="到账类型" size="small">
            <el-select v-model="searchForm.costType" placeholder="请选择到账类型">
              <el-option
                v-for="item in global.otherfeesList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="备注说明" size="small">
            <el-input v-model="searchForm.remark"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <!--<el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>-->
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
          <el-form-item label="是否内部划转" size="small">
            <el-switch
              v-model="teamRow.inscription"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value=1
              :inactive-value=0
              :disabled="isInscription"
            >
            </el-switch>
          </el-form-item>
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input type="text" v-model="teamRow.projectName"></el-input>
          </el-form-item>
          <el-form-item prop="inTeamName" label="到账团队" size="small">
            <el-autocomplete
              v-model="teamRow.inTeamName"
              :fetch-suggestions="querySearchInTeam"
              @select="handleSelectInTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="到账金额" size="small">
            <el-input type="number" v-model.number="teamRow.amount">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
          <el-form-item prop="date" label="到账时间" size="small">
            <el-date-picker
              v-model="teamRow.date"
              type="date"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="银行名称" size="small">
            {{teamRow.bankName}}
          </el-form-item>
          <el-form-item v-if="teamRow.inscription" prop="outTeamName" label="支出团队" size="small">
            <el-autocomplete
              v-model="teamRow.outTeamName"
              :fetch-suggestions="querySearchOutTeam"
              @select="handleSelectOutTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="经办人" size="small">
            <el-autocomplete
              v-model="teamRow.agent"
              :fetch-suggestions="querySearchOperator"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
      @openVisible="openVisible"
      :show-summary="true"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="80">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.managementFeeId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="文件查看" :visible.sync="imgVisible" width="600px">
      <img class="img-dialog" :src="imgMsg.src" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    selectOtherManagementFee,
    addOtherManagementFee,
    updateOtherManagementFee,
    deleteOtherManagementFee,
    selectTeam,
    selectOperator
  } from '@/api/otherfees'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const costType = function(row, column) {
    if (row.costType) {
      let obj = {}
      obj = global.otherfeesList.find((item) => {
        return item.value === row.costType // 筛选出匹配数据
      })
      return obj.label
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'projectName', label: '项目名称', minWidth: '35%', fixed: true },
          { prop: 'inTeamName', label: '到账团队', minWidth: '20%' },
          { prop: 'amount', label: '到账金额(万元)', width: '100' },
          { prop: 'costType', label: '到账类型', width: '150', formatter: costType },
          { prop: 'date', label: '到账时间', width: '100', component: time },
          { prop: 'bankName', label: '银行名称', width: '150' },
          { prop: 'outTeamName', label: '支出团队', minWidth: '20%' },
          { prop: 'agent', label: '经办人', width: '100' },
          { prop: 'remark', label: '备注说明', minWidth: '20%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        costTypes: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增其他到账',
        dialogType: 0,
        teamRow: {
          inscription: 0
        },
        teamRowClear: {
          inscription: 0
        },
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'change' }
          ],
          outTeamName: [
            { required: true, message: '请输入支出团队', trigger: 'change' }
          ],
          inTeamName: [
            { required: true, message: '请输入收入团队', trigger: 'change' }
          ],
          date: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ]
        },
        visible: false,
        imgVisible: false,
        imgMsg: {},
        isInscription: false // 修改时禁用开关
      }
    },
    computed: {
      global() {
        return global
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        if (!this.searchFormList.costType) {
          this.searchFormList.costType = 2
        }
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          console.log(this.tableData)
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        this.outerVisible = true
        this.isInscription = false
        this.dialogTitle = '新增其他到账'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.dialogType = 1
      },
      editTeam(managementFeeId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.isInscription = true
        this.dialogTitle = '修改其他到账'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          managementFeeId: managementFeeId
        }
        selectOtherManagementFee(params).then(response => {
          this.teamRow = Object.assign({}, response.responseBody)
        })
      },
      submitForm(formName) {
        const params = {
          inscription: this.teamRow.inscription,
          projectName: this.teamRow.projectName,
          amount: this.teamRow.amount || 0,
          date: this.teamRow.date,
          inTeamId: this.teamRow.inTeamId,
          inTeamName: this.teamRow.inTeamName,
          agent: this.teamRow.agent || '',
          remark: this.teamRow.remark || ''
        }
        if (this.teamRow.inscription) {
          params.outTeamId = this.teamRow.outTeamId
          params.outTeamName = this.teamRow.outTeamName
        }
        if (!this.dialogType) {
          params.managementFeeId = this.teamRow.managementFeeId
        }
        if (this.teamRow.inscription && !this.teamRow.outTeamId) {
          this.$message({
            message: '该支出团队不存在',
            type: 'warning'
          })
        } else if (!this.teamRow.inTeamId) {
          this.$message({
            message: '该收入团队不存在',
            type: 'warning'
          })
        } else {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.dialogType) {
                addOtherManagementFee(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              } else {
                updateOtherManagementFee(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          managementFeeId: scope.row.managementFeeId,
          outTeamId: scope.row.outTeamId,
          inTeamId: scope.row.inTeamId,
          amount: scope.row.amount,
          isDel: 1
        }
        deleteOtherManagementFee(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      getFile(event) {
        this.teamRow.credentials = event.target.files[0]
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      },
      querySearchOperator(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        if (queryString) {
          const params = {
            teamId: this.teamRow.teamId,
            name: queryString
          }
          selectOperator(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.name
            }
            let projectList = response.responseBody
            if (projectList.length === 0) {
              projectList = [{ value: '暂无数据' }]
            }
            callback(projectList)
          })
        } else {
          callback([])
        }
      },
      querySearchFormOutTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.searchForm.outTeamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectFormOutTeam(item) {
        this.$set(this.searchForm, 'outTeamId', item.teamId)
        this.$set(this.searchForm, 'outTeamName', item.teamName)
      },
      querySearchFormInTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.searchForm.inTeamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectFormInTeam(item) {
        this.$set(this.searchForm, 'inTeamId', item.teamId)
        this.$set(this.searchForm, 'inTeamName', item.teamName)
      },
      querySearchOutTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.outTeamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectOutTeam(item) {
        this.$set(this.teamRow, 'outTeamId', item.teamId)
        this.$set(this.teamRow, 'outTeamName', item.teamName)
      },
      querySearchInTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.inTeamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectInTeam(item) {
        this.$set(this.teamRow, 'inTeamId', item.teamId)
        this.$set(this.teamRow, 'inTeamName', item.teamName)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .img-dialog {
      max-width: 540px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
